#include "stdafx.h"
#include "wf_drawlineparallel.h"

#include "pf_creation.h"
#include "pf_snapper.h"
#include "wf_drawlineparallelthrough.h"



WF_DrawLineParallel::WF_DrawLineParallel(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw Parallels", container, graphicView) {

    parallel = NULL;
    entity = NULL;
    distance = 1.0;
    number = 1;
    coord = PF_Vector(false);
}

QAction* WF_DrawLineParallel::createGUIAction(LX_Define::ActionType type, QObject* /*parent*/) {

    QAction* action = NULL;

    if (type==LX_Define::ActionDrawLineParallel) {
		// tr("Para&llel"),
        action = new QAction(tr("Parallel"), NULL);
		action->setIcon(QIcon(":/extui/linespara.png"));
    } else if (type==LX_Define::ActionDrawArcParallel) {
        action = new QAction(tr("Concentric"), NULL);
		action->setIcon(QIcon(":/extui/arcspara.png"));
    } else if (type==LX_Define::ActionDrawCircleParallel) {
        action = new QAction(tr("Concentric"), NULL);
		action->setIcon(QIcon(":/extui/circlespara.png"));
    }
    //action->zetStatusTip(tr("Draw parallels to existing lines, arcs, "circles"));
    return action;
}


void WF_DrawLineParallel::trigger() {
    PF_PreviewActionInterface::trigger();

    PF_Creation creation(container, graphicView);
    PF_Entity* e = creation.createParallel(coord,
                                           distance, number,
                                           entity);

    if (e==NULL) {
        PF_DEBUG->print("PF_ActionDrawLineParallel::trigger:"
                        " No parallels added\n");
    }
}



void WF_DrawLineParallel::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawLineParallel::mouseMoveEvent begin");

    coord = PF_Vector(graphicView->toGraphX(e->x()),
                      graphicView->toGraphY(e->y()));

    entity = catchEntity(e, LX_Define::ResolveAll);

    switch (getStatus()) {
    case SetEntity: {
            deletePreview();

            PF_Creation creation(preview, NULL, false);
            creation.createParallel(coord,
                                    distance, number,
                                    entity);

            drawPreview();
        }
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionDrawLineParallel::mouseMoveEvent end");
}



void WF_DrawLineParallel::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::RightButton) {
        init(getStatus()-1);
    } else {
        trigger();
    }
}



void WF_DrawLineParallel::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetEntity:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify Distance <%1> or select entity or [%2]")
                .arg(distance).arg(PF_COMMANDS->command("through")),
                tr("Cancel"));
            break;

        case SetNumber:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Enter number:"), "");
            break;

        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DrawLineParallel::showOptions() {
    PF_ActionInterface::showOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true);
    }
    updateMouseButtonHints();
}



void WF_DrawLineParallel::hideOptions() {
    PF_ActionInterface::hideOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, false);
    }
}



void WF_DrawLineParallel::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    switch (getStatus()) {
    case SetEntity: {
            if (checkCommand("through", c)) {
                finish();
                graphicView->setCurrentAction(
                    new WF_DrawLineParallelThrough(*container,
                                                         *graphicView));
            } else if (checkCommand("number", c)) {
                deletePreview();
                setStatus(SetNumber);
            } else {
                bool ok;
                double d = PF_Math::eval(c, &ok);
                if (ok==true && d>1.0e-10) {
                    distance = d;
                } else {
                    if (PF_DIALOGFACTORY!=NULL) {
                        PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                    }
                }
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->requestOptions(this, true, true);
                }
                updateMouseButtonHints();
                //setStatus(SetEntity);
            }
        }
        break;

    case SetNumber: {
            bool ok;
            int n = c.toInt(&ok);
            if (ok==true) {
                if (n>0 && n<100) {
                    number = n;
                } else {
                    if (PF_DIALOGFACTORY!=NULL) {
                        PF_DIALOGFACTORY->commandMessage(tr("Not a valid number. "
                                                            "Try 1..99"));
                    }
                }
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
            if (PF_DIALOGFACTORY!=NULL) {
                PF_DIALOGFACTORY->requestOptions(this, true, true);
            }
            setStatus(SetEntity);
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_DrawLineParallel::getAvailableCommands() {
    PF_StringList cmd;

    switch (getStatus()) {
    case SetEntity:
        cmd += command("number");
        cmd += command("through");
        break;
    default:
        break;
    }

    return cmd;
}



void WF_DrawLineParallel::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawLineParallel::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarLines);
    }
}


// EOF
